package com.online_exam.controller.admin;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.online_exam.base.RestResponse;
import com.online_exam.domain.Message;
import com.online_exam.domain.MessageUser;
import com.online_exam.service.MessageService;

import com.online_exam.service.UserService;
import com.online_exam.util.JwtUtil;
import com.online_exam.viewmodel.admin.message.MessagePage;
import com.online_exam.viewmodel.admin.message.MessagePageRequestVM;
import com.online_exam.viewmodel.admin.message.MessageResponseVM;
import com.online_exam.viewmodel.admin.message.MessageSendVM;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping(value = "/api/admin/message")
public class MessageController {

    private MessageService messageService;
    private ModelMapper modelMapper;
    private UserService userService;
    @Autowired
    public MessageController(MessageService messageService, ModelMapper modelMapper,UserService userService) {
        this.messageService = messageService;
        this.modelMapper = modelMapper;
        this.userService=userService;
    }


    @PostMapping(value = "/send")
    public RestResponse sendMsg(@RequestBody @Valid MessageSendVM messageSendVM, HttpServletRequest request) {
//        int id= JwtUtil.DecodeByToken(request,userService).getId();//这里没办法测试，没有管理员账号，因此只能写没办法测
        int index = messageService.insertMessageByVM(messageSendVM, 2);//注意这里后面要改一下，userid是从token获取
        if (index>0){
            System.out.println("信息发送成功~~");
        }else{
            System.out.println("出现问题~");
            return RestResponse.fail(111,"操作出错");
        }
        return RestResponse.ok();
    }

    @PostMapping(value = "/page")
    public RestResponse<MessagePage> pages(@RequestBody MessagePageRequestVM vm){
        System.out.println(">>>>>>>>>>>>>>");
        Page<Object> page = PageHelper.startPage(vm.getPageIndex(), vm.getPageSize());
        List<Message> list = messageService.select(vm);
        MessagePage messagePage=new MessagePage();
        messagePage.setPageIndex(page.getPageNum());
        messagePage.setPageSize(page.getPageSize());

        //得到所有信息的id
        List<Integer> ids = list.stream().map((message -> message.getId())).collect(Collectors.toList());
        //根据id得到所有接受者的信息
        List<MessageUser> userList = ids.size()==0? null : messageService.selectByMsgId(ids);
        if (null==userList){
            return RestResponse.fail(101,"没有发送过信息");
        }
        //转成视图类
        List<MessageResponseVM> vmList=list.stream().map((p->{
            MessageResponseVM responseVM = modelMapper.map(p, MessageResponseVM.class);
            String receives = userList.stream().filter(messageUser -> {
                return messageUser.getMessageId().equals(p.getId());
            }).map(name -> {
                return name.getReceiveUserName();
            }).collect(Collectors.joining(","));
            responseVM.setCreateTime(p.getCreateTime().toString());
            responseVM.setReceives(receives);
            responseVM.setReadCount(p.getReadCount());
            responseVM.setReceiveUserCount(p.getReceiveUserCount());
            return responseVM;
        })).collect(Collectors.toList());
        messagePage.setList(vmList);

        return RestResponse.ok(messagePage);
    }
}
